<?php 
session_start();
require_once ("class/m_login.php");

	$m = new M_login();
	if (isset($_POST['m'])  &&  $_POST['m']!=""){
		if ($_POST['m']== 'c'){
			if(isset($_POST['username']) && $_POST['username']!="" && $_POST['password'] !="" && isset($_POST['password'])){ 
				$name=$_POST['username'];
				$password=$_POST['password'];
				//$remember=$_POST['isremember'];
				
				$val = $m->checkUserLogin($name,$password);
				if ($val=="1"){
						//get session data
						$rows=$m->getUserDetail();
						//clear session
						session_unregister("username");
						session_unregister("custid");
						session_unregister("userid");
						session_unregister("bid");
						session_unregister("branchtype"); 
						session_unregister("groupid"); 
						session_unregister("discount"); 
						session_unregister("branchcode"); 
						session_unregister("state");
						session_unregister("permission");
						//Declare session
						session_register("username");
						session_register("custid");
						session_register("userid");
						session_register("bid");
						session_register("discount");
						session_register("branchtype");
						session_register("groupid");
						session_register("state");
						session_register("branchcode"); 
						session_register("permission"); 
						//Set
						$_SESSION['username'] = $rows['UserName'];
						$_SESSION['branchcode'] = $rows['BranchCode'];
						$_SESSION['userid'] = $rows['UserID'];
						$_SESSION['bid'] = $rows['BranchID'];
						$_SESSION['discount'] = $rows['Discount'];
						$_SESSION['branchtype'] = $rows['isHo'];
						$_SESSION['groupid'] = $rows['GroupID'];
						$_SESSION["state"]="Authen";
						
						$result= array('msg'=> "Success",
												'result'=>$val);
					 
						$_SESSION["permission"]=$m->CheckUserAuthen($rows['UserID']);
						
						
				}else if ($val=="2"){
					$result= array('msg'=>"Incorrect password.",
										'result'=>$val);
				}else {
					$result= array('msg'=>"Incorrect username.",
								'result'=>$val);
				}
			//	echo "Success";
				echo json_encode($result);
			}else{
				echo "Errro";
			}
		} 
	}else{
	
		echo"Error2";
		
		}
?>